package com.baizhi.service;

import com.baizhi.dao.UserDao;
import com.baizhi.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Map<String, Object> queryAllPage(int page, int size) {
        HashMap<String,Object> map=new HashMap<>();
        List<User> list = userDao.queryAllPage((page - 1) * size, size);
        //page：当前页
        //count：总页数
        int count = userDao.queryCount();
        //存储的总页数 = 每页显示的条数/总条数
        int pageCount=count%size==0?count/size:count/size+1;
        //当前页数
        map.put("page",page);
        //分页查的数据
        map.put("data",list);
        //总页数
        map.put("pagenum",pageCount);
        //总条数
        map.put("count",count);
        return map;
    }

    @Override
    public void updateStatus(int status, String id) {
        userDao.updateStatus(status,id);
    }

    @Override
    public void deleteUser(String id) {
        userDao.deleteUser(id);
    }

    @Override
    public void addUser(MultipartFile file, User user) {
        user.setId(UUID.randomUUID().toString().replace("-",""));
        user.setCreatdate(new Date());
        user.setStatus(1);
        userDao.addUser(user);
    }
}
